package com.akuhome.resource.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class LogAspect {
    private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(public * com.akuhome.resource.controller.*Controller.*(..))")
    public void logPointcut(){

    }

    @Before("logPointcut()")
    public void Before(JoinPoint joinPoint){
        ServletRequestAttributes attributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //url
        logger.info("url={}",request.getRequestURL());
        //method
        logger.info("method={}",request.getMethod());
        //ip
        logger.info("ip={}",request.getRemoteAddr());
        //class
        logger.info("class_method={}",joinPoint.getSignature().getName());
    }

    @After("logPointcut()")
    public void After(){
    }

    @AfterReturning(pointcut = "logPointcut()",returning = "object")
    public void afterReturning(Object object){
        logger.info("return={}",object);
    }
}
